package com.spark.rectify

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import com.spark.common.BaseFragment
import com.spark.common.helper.MutableStatePagerAdapter
import kotlinx.android.synthetic.main.frag_handle.*

/**
 * @info 处理流程状态
 *   -> 草稿
 *   -> 审核中
 *   -> 被驳回
 *   -> 确认中
 *   -> 被申诉
 *   -> 学习中
 *   -> 待存储
 */
class RectProcedureFragment : BaseFragment() {
    private lateinit var rectProcedureAdapter: MutableStatePagerAdapter

    companion object {
        fun newInstance(data: String): RectProcedureFragment {
            val target = RectProcedureFragment()
            val args = Bundle()
            args.putString("data", data)
            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_handle
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        /**
         * -> 草稿
         *      纠违信息 RectifyInfoFragment
         *      处罚单预览 PunishOrderPreviewFragment
         * -> 审核中
         *      纠违信息 RectifyInfoFragment
         *      处罚单预览 PunishOrderPreviewFragment
         * -> 被驳回
         *      纠违信息 RectifyInfoFragment (可以关闭)
         *      处罚单预览 PunishOrderPreviewFragment
         * -> 确认中
         *      纠违信息 RectifyInfoFragment (可以关闭)
         *      处罚单预览 PunishOrderPreviewFragment
         * -> 被申诉
         *      处罚单预览 PunishOrderPreviewFragment
         *      调整处罚 AdjustPunishFragment
         * -> 学习中
         *      执行情况 ExecuteInfoFragment
         *      处罚单预览 PunishOrderPreviewFragment
         * -> 待存储
         *      执行情况 ExecuteInfoFragment
         *      处罚单预览 PunishOrderPreviewFragment
         */
        val tabTitles = mutableListOf<String>()
        val frags = mutableListOf<Fragment>()
        val state = 1
        when (state) {
            // 草稿
            // 审核中
            // 被驳回
            // 确认中
            0, 1, 2, 3 -> {
                tabTitles.apply {
                    add("违规信息")
                    add("处罚单预览")
                }
                frags.apply {
                    add(RectInfoFragment.newInstance())
                    add(PunishOrderPreviewFragment.newInstance())
                }
            }
            // 被申诉
            4 -> {
                tabTitles.apply {
                    add("处罚单预览")
                    add("调整处罚")
                }
                frags.apply {
                    add(PunishOrderPreviewFragment.newInstance())
                    add(AdjustPunishFragment.newInstance())
                }
            }
            // 学习中
            // 待存储
            5, 6 -> {
                tabTitles.apply {
                    add("执行情况")
                    add("处罚单预览")
                }
                frags.apply {
                    add(ExecuteInfoFragment.newInstance())
                    add(PunishOrderPreviewFragment.newInstance())
                }
            }
        }
        tabTitles.forEach {
            tabs.addTab(tabs.newTab().setText(it))
        }
        rectProcedureAdapter = object : MutableStatePagerAdapter(childFragmentManager) {
            override fun getCount(): Int {
                return tabTitles.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return tabTitles[position]
            }
        }
        rectProcedureAdapter.adds(frags)
        pager.apply {
            adapter = rectProcedureAdapter
            canScroll = false
        }
        tabs.setupWithViewPager(pager)
    }
}